/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.gui;

import java.awt.event.ItemEvent;
import sk.yw.azetclient.Main;

/**
 *
 * @author  error216
 */
public class AuthenticationDialog extends javax.swing.JDialog {
    
    private static final long serialVersionUID = 318026520l;
    
    /** Creates new form AuthenticationDialog */
    public AuthenticationDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        usernameField = new javax.swing.JTextField();
        usernameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        savePasswordCheckBox = new javax.swing.JCheckBox();
        automaticAuthenticationCheckBox = new javax.swing.JCheckBox();
        authenticationButton = new javax.swing.JButton();

        setTitle(Main.getText("authenticationDialog.title"));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                AuthenticationDialog.this.windowClosing(evt);
            }
        });

        usernameField.setText(Main.getProperty("azetclient.username"));
        usernameField.setSelectionEnd(usernameField.getText().length());
        usernameField.setSelectionStart(0);
        usernameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                authenticate(evt);
            }
        });

        usernameLabel.setLabelFor(usernameField);
        usernameLabel.setText(Main.getText("authenticationDialog.username"));

        passwordLabel.setText(Main.getText("authenticationDialog.password"));

        passwordField.setText(Main.getProperty("azetclient.password"));
        passwordField.setSelectionEnd(passwordField.getPassword().length);
        passwordField.setSelectionStart(0);
        passwordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                authenticate(evt);
            }
        });

        savePasswordCheckBox.setSelected("true".equals(Main.getProperty("azetclient.savePassword")));
        savePasswordCheckBox.setText(Main.getText("authenticationDialog.savePassword"));
        savePasswordCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                savePasswordCheckBoxItemStateChanged(evt);
            }
        });

        automaticAuthenticationCheckBox.setSelected("true".equals(Main.getProperty("azetclient.automaticAuthentication")));
        automaticAuthenticationCheckBox.setText(Main.getText("authenticationDialog.automaticAuthentication"));
        automaticAuthenticationCheckBox.setEnabled(savePasswordCheckBox.isSelected());

        authenticationButton.setText(Main.getText("authenticationDialog.authenticate"));
        authenticationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                authenticate(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(passwordLabel)
                            .addComponent(usernameLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(passwordField)
                            .addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)))
                    .addComponent(automaticAuthenticationCheckBox)
                    .addComponent(savePasswordCheckBox)
                    .addComponent(authenticationButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameLabel)
                    .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addComponent(savePasswordCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(automaticAuthenticationCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(authenticationButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void authenticate(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_authenticate
        setFieldsEnabled(false);
        String username = usernameField.getText();
        Main.setProperty("azetclient.username", username);
        
        String password = new String(passwordField.getPassword());
        if (savePasswordCheckBox.isSelected()) {
            Main.setProperty("azetclient.savePassword", "true");
            Main.setProperty("azetclient.password", password);
        } else {
            Main.setProperty("azetclient.savePassword", "false");
            Main.setProperty("azetclient.password", "");
        }
        if (automaticAuthenticationCheckBox.isSelected()) {
            Main.setProperty("azetclient.automaticAuthentication", "true");
        } else {
            Main.setProperty("azetclient.automaticAuthentication", "false");
        }
        
        ((MainFrame) getParent()).authenticate(username, password);
    }//GEN-LAST:event_authenticate

    private void savePasswordCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_savePasswordCheckBoxItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            automaticAuthenticationCheckBox.setEnabled(true);
        }
        if (evt.getStateChange() == ItemEvent.DESELECTED) {
            automaticAuthenticationCheckBox.setSelected(false);
            automaticAuthenticationCheckBox.setEnabled(false);
        }
    }//GEN-LAST:event_savePasswordCheckBoxItemStateChanged

    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
        System.exit(0);
}//GEN-LAST:event_windowClosing
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton authenticationButton;
    private javax.swing.JCheckBox automaticAuthenticationCheckBox;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JCheckBox savePasswordCheckBox;
    private javax.swing.JTextField usernameField;
    private javax.swing.JLabel usernameLabel;
    // End of variables declaration//GEN-END:variables
    
    public void setFieldsEnabled(boolean enabled) {
        usernameField.setEnabled(enabled);
        passwordField.setEnabled(enabled);
        savePasswordCheckBox.setEnabled(enabled);
        if (savePasswordCheckBox.isSelected()) {
            automaticAuthenticationCheckBox.setEnabled(enabled);
        }
        authenticationButton.setEnabled(enabled);
        
        if (enabled) {
            authenticationButton.setText(
                    Main.getText("authenticationDialog.authenticate"));
        } else {
            authenticationButton.setText(
                    Main.getText("authenticationDialog.authenticating"));
        }
    }
}
